home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Resources / Online / Term / Extras / Source / gtlayout-source.lha / LTP_CreateMenuTemplate.c < prev    next >
C/C++ Source or Header  |  1996-03-18  |  5KB  |  260 lines

  1. /*
  2. **    GadTools layout toolkit
  3. **
  4. **    Copyright © 1993-1996 by Olaf `Olsen' Barthel
  5. **        Freely distributable.
  6. **
  7. **    :ts=4
  8. */
  9.  
  10. #ifndef _GTLAYOUT_GLOBAL_H
  11. #include "gtlayout_global.h"
  12. #endif
  13.  
  14. #ifdef DO_MENUS
  15.  
  16.     /* LTP_CreateMenuTemplate(RootMenu *Root,LONG *ErrorPtr,struct NewMenu *MenuTemplate):
  17.      *
  18.      *    Create the menu strip data.
  19.      */
  20.  
  21. BOOL
  22. LTP_CreateMenuTemplate(RootMenu *Root,LONG *ErrorPtr,struct NewMenu *MenuTemplate)
  23. {
  24.     MenuNode    *LastMenu    = NULL;
  25.     ItemNode    *LastItem    = NULL;
  26.     ItemNode    *LastSub    = NULL;
  27.  
  28.     MenuNode    *Menu;
  29.     ItemNode    *Item;
  30.     ItemNode    *Sub;
  31.  
  32.     LONG         Error;
  33.  
  34.     BOOL         Done;
  35.  
  36.     Done    = FALSE;
  37.     Error    = 0;
  38.  
  39.     do
  40.     {
  41.             // Check the type
  42.  
  43.         switch(MenuTemplate->nm_Type)
  44.         {
  45.                 // Stop here?
  46.  
  47.             case NM_END:
  48.  
  49.                 DB(kprintf("NM_END\n"));
  50.  
  51.                     // Mark the last menu item
  52.  
  53.                 if(LastSub)
  54.                     LastSub->Flags |= ITEMF_LastItem;
  55.                 else
  56.                 {
  57.                     if(LastItem)
  58.                         LastItem->Flags |= ITEMF_LastItem;
  59.                 }
  60.  
  61.                 Done = TRUE;
  62.                 break;
  63.  
  64.                 // Found a new menu?
  65.  
  66.             case NM_TITLE:
  67.  
  68.                 DB(kprintf("NM_TITLE |%s|\n",MenuTemplate->nm_Label));
  69.  
  70.                     // Stop on weird labels
  71.  
  72.                 if(MenuTemplate->nm_Label == NM_BARLABEL)
  73.                     Error = ERROR_OBJECT_WRONG_TYPE;
  74.                 else
  75.                 {
  76.                         // Choose the menu to use
  77.  
  78.                     if(!LastMenu)
  79.                         Menu = (MenuNode *)&Root->Node;
  80.                     else
  81.                         Menu = NULL;
  82.  
  83.                     DB(kprintf("         make menu\n"));
  84.  
  85.                         // Make room for the menu
  86.  
  87.                     if(Menu = LTP_MakeMenu(Root,Menu,MenuTemplate))
  88.                     {
  89.                             // Link to last menu
  90.  
  91.                         if(LastMenu)
  92.                             LastMenu->Menu.NextMenu = &Menu->Menu;
  93.  
  94.                             // Mark the last menu item
  95.  
  96.                         if(LastSub)
  97.                             LastSub->Flags |= ITEMF_LastItem;
  98.                         else
  99.                         {
  100.                             if(LastItem)
  101.                                 LastItem->Flags |= ITEMF_LastItem;
  102.                         }
  103.  
  104.                         LastMenu    = Menu;
  105.                         LastItem    = NULL;
  106.                         LastSub        = NULL;
  107.  
  108.                             // Add the menu to the list
  109.  
  110.                         AddTail((struct List *)&Root->MenuList,(struct Node *)Menu);
  111.                     }
  112.                     else
  113.                         Error = ERROR_NO_FREE_STORE;
  114.                 }
  115.  
  116.                 break;
  117.  
  118.                 // Found a new menu item?
  119.  
  120.             case NM_ITEM:
  121.  
  122.                 DB(kprintf("NM_ITEM |%s|\n",MenuTemplate->nm_Label == NM_BARLABEL ? (STRPTR)"«Bar»" : MenuTemplate->nm_Label));
  123.  
  124.                     // We need a menu to attach this to
  125.  
  126.                 if(!LastMenu)
  127.                     Error = ERROR_INVALID_COMPONENT_NAME;
  128.                 else
  129.                 {
  130.                     DB(kprintf("        make item\n"));
  131.  
  132.                         // Make room for the item
  133.  
  134.                     if(Item = LTP_MakeItem(Root,MenuTemplate))
  135.                     {
  136.                             // Link it in
  137.  
  138.                         if(LastItem)
  139.                             LastItem->Item.NextItem = &Item->Item;
  140.                         else
  141.                             LastMenu->Menu.FirstItem = &Item->Item;
  142.  
  143.                         LastItem    = Item;
  144.                         LastSub        = NULL;
  145.  
  146.                             // Add it to the item list
  147.  
  148.                         AddTail((struct List *)&Root->ItemList,(struct Node *)Item);
  149.                     }
  150.                     else
  151.                         Error = ERROR_NO_FREE_STORE;
  152.                 }
  153.  
  154.                 break;
  155.  
  156.             case NM_SUB:
  157.  
  158.                 DB(kprintf("NM_SUB |%s|\n",MenuTemplate->nm_Label == NM_BARLABEL ? (STRPTR)"«Bar»" : MenuTemplate->nm_Label));
  159.  
  160.                     // We need a menu item to attach this to
  161.  
  162.                 if(!LastItem)
  163.                     Error = ERROR_INVALID_COMPONENT_NAME;
  164.                 else
  165.                 {
  166.                     DB(kprintf("       make sub\n"));
  167.  
  168.                         // Make room for the item
  169.  
  170.                     if(Sub = LTP_MakeItem(Root,MenuTemplate))
  171.                     {
  172.                             // Make sure it stays a submenu item
  173.  
  174.                         Sub->Flags |= ITEMF_IsSub;
  175.  
  176.                             // Link it in
  177.  
  178.                         if(LastSub)
  179.                             LastSub->Item.NextItem = &Sub->Item;
  180.                         else
  181.                         {
  182.                                 // We cannot attach submenu items to
  183.                                 // separator bars
  184.  
  185.                             if(LastItem->Flags & ITEMF_IsBar)
  186.                                 Error = ERROR_INVALID_COMPONENT_NAME;
  187.                             else
  188.                             {
  189.                                 DB(kprintf("-----------> |%s| has submenu items\n",((struct IntuiText *)LastItem->Item.ItemFill)->IText));
  190.  
  191.                                     // Link the submenu item in
  192.  
  193.                                 LastItem->Item.SubItem = &Sub->Item;
  194.  
  195.                                     // Zap the command sequence data
  196.  
  197.                                 LastItem->Flags            = (LastItem->Flags & ~ITEMF_Command) | ITEMF_HasSub;
  198.                                 LastItem->ExtraLabel    = NULL;
  199.  
  200.                                 LastItem->Item.Flags    &= ~COMMSEQ;
  201.                                 LastItem->Item.Command     = 0;
  202.  
  203.                                     // This is the first submenu item for this menu item
  204.  
  205.                                 Sub->Flags |= ITEMF_FirstSub;
  206.                             }
  207.                         }
  208.  
  209.                         LastSub = Sub;
  210.  
  211.                             // Add it to the list
  212.  
  213.                         AddTail((struct List *)&Root->ItemList,(struct Node *)Sub);
  214.                     }
  215.                     else
  216.                         Error = ERROR_NO_FREE_STORE;
  217.                 }
  218.  
  219.                 // Fall through to...
  220.  
  221.                 // Do nothing
  222.  
  223.             case NM_IGNORE:
  224.  
  225.                 break;
  226.         }
  227.  
  228.             // Proceed to the next entry
  229.  
  230.         MenuTemplate++;
  231.     }
  232.     while(!Done && !Error);
  233.  
  234.     DB(kprintf("%s error = %ld\n",__FILE__,Error));
  235.  
  236.         // Did we create anything at all?
  237.  
  238.     if(!Error)
  239.     {
  240.         if(!Root->MenuList.mlh_Head->mln_Succ)
  241.             Error = ERROR_OBJECT_NOT_FOUND;
  242.         else
  243.             LTP_FixExtraLabel(Root,&Error);
  244.     }
  245.  
  246.     DB(kprintf("through, error=%ld\n",Error));
  247.  
  248.     if(Error)
  249.     {
  250.         if(ErrorPtr)
  251.             *ErrorPtr = Error;
  252.  
  253.         return(FALSE);
  254.     }
  255.     else
  256.         return(TRUE);
  257. }
  258.  
  259. #endif    /* DO_MENUS */
  260.